package com.sduept.nwld.dataserver.controller.statistics;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONArray;
import com.sduept.bigdata.document.core.CommonDocumentProperties;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.manager.countanalyse.ProtectKpiAnalysisManger;
import com.sduept.nwld.dataserver.manager.statistics.ProtectKpiCountManager;
import com.sduept.nwld.dataserver.manager.statistics.ProtectKpiManager;
import com.sduept.nwld.dataserver.manager.statistics.StatisticsCommonManager;
import com.sduept.nwld.dataserver.model.countanalyse.FaultFastRemove;
import com.sduept.nwld.dataserver.model.countanalyse.ProtectCorrectAct;

@Named
@ViewScoped
public class ProtectKpiController extends AbstractController {

	private static final long serialVersionUID = 247778787034594666L;

	@Autowired
	private ProtectKpiManager manager;
	@Autowired
	private ProtectKpiAnalysisManger analysisM;
	@Autowired
	private ProtectKpiCountManager countM;
	@Autowired
	private StatisticsCommonManager commonM;
	@Autowired
	private CommonDocumentProperties pros;
	//展示页面
	private Integer year;
	private Integer month;
	private List<Integer> months = new ArrayList<Integer>();
	private Integer voltage = 500;
	private List<Integer> voltages = new ArrayList<Integer>();
	private int unitType = 0;//单位类型（0：检修单位，1：调管单位）
	private String[] selectedMaintenance = new String[] {"超高压公司", "调峰调频公司", "广东电网", "广西电网", "广州电网", "贵州电网", "海南电网",
			"深圳电网", "云南电网"};//前台选择的检修单位

	//维护页面
	private Integer mYear;
	private Integer mMonth;
	private Integer mVoltage;
	private Integer queryType = 1;//0是年指标  1是月指标
	//	private Integer selectType = 1;//0是年指标  1是月指标
	private String subCompany;
	private List<String> subCompanys = new ArrayList<String>();
	private boolean covered = false;//是否覆盖修改数据，是：统计时覆盖人工修改的数据；否：统计时不覆盖人工修改的数据。

	private List<ProtectCorrectAct> protectCorrectActs = new ArrayList<>();
	private ProtectCorrectAct selectProtectCorrectAct = new ProtectCorrectAct();
	private ProtectCorrectAct protectCorrectAct = new ProtectCorrectAct();

	private List<FaultFastRemove> faultFastRemoves = new ArrayList<FaultFastRemove>();
	private FaultFastRemove selectFaultFastRemove = new FaultFastRemove();
	private FaultFastRemove faultFastRemove = new FaultFastRemove();

	private String chartJsonData;

	private StreamedContent templateFile;

	@PostConstruct
	public void init() {
		Calendar cal = Calendar.getInstance();
		year = cal.get(Calendar.YEAR);
		month = cal.get(Calendar.MONTH)+1;
		for (int i = 1; i <= 12; i++) {
			months.add(i);
		}
		voltages = commonM.getVoltages();
		//		subCompanys = commonM.getSubCompanys();
		subCompanys = commonM.getAllUnits(unitType);//从公用接口获取
		unitType = 0;
		queryChartData();
	}

	/**
	 * 进入数据维护页面时，默认显示主页面上的查询条件
	 */
	public void initQueryActData() {
		mYear = year;
		mMonth = month;
		mVoltage = voltage;
		subCompany = "全部";
		subCompanys = commonM.getAllUnits(unitType);
		covered = false;
		queryProtectCorrectAct();
	}

	public void initQueryRemoveData() {
		mYear = year;
		mMonth = month;
		mVoltage = voltage;
		subCompany = "全部";
		subCompanys = commonM.getAllUnits(unitType);
		covered = false;
		queryFaultFastRemove();
	}

	public void queryProtectCorrectAct() {
		String s = "全部".equals(subCompany)? null:subCompany;
		Integer v = mVoltage == 0? null:mVoltage;
		selectProtectCorrectAct = null;
		protectCorrectActs.clear();
		if(queryType == 0) {
			// 查询年指标，
			if(unitType == 0) {//检修单位，有默认选中的单位名称
				protectCorrectActs.addAll(manager.queryYearProtectCorrectActByCondition(mYear, s, v, unitType, Arrays.asList(selectedMaintenance)));
			}else{
				protectCorrectActs.addAll(manager.queryYearProtectCorrectActByCondition(mYear, s, v, unitType));
			}
		} else {
			//查询月指标
			Integer m = mMonth == 0?null:mMonth;
			if(unitType == 0) {//检修单位，有默认选中的单位名称
				protectCorrectActs.addAll(manager.queryMonthProtectCorrectActByCondition(mYear, m, s, v, unitType, Arrays.asList(selectedMaintenance)));
			}else {
				protectCorrectActs.addAll(manager.queryMonthProtectCorrectActByCondition(mYear, m, s, v, unitType));
			}
		}
	}

	public void resetProtectCorrectAct() {
		initQueryCondition();
		queryProtectCorrectAct();
	}
	/**
	 * 初始化查询条件
	 */
	private void initQueryCondition() {
		Calendar cal = Calendar.getInstance();
		mYear = cal.get(Calendar.YEAR);
		mMonth = cal.get(Calendar.MONTH)+1;
		mVoltage = 0;
		queryType = 1; // 月指标
		subCompany = "全部";
		unitType = 0;
	}
	public void queryFaultFastRemove() {
		String s = "全部".equals(subCompany)? null:subCompany;
		Integer v = mVoltage == 0? null:mVoltage;
		selectFaultFastRemove = null;
		faultFastRemoves.clear();
		if(queryType == 0) {
			// 查询年指标，
			if(unitType == 0) {//检修单位，有默认选中的单位名称
				faultFastRemoves.addAll(manager.queryYearFaultFastRemoveByCondition(mYear, s, v, unitType, Arrays.asList(selectedMaintenance)));
			}else {
				faultFastRemoves.addAll(manager.queryYearFaultFastRemoveByCondition(mYear, s, v, unitType));
			}
		}else {
			// 查询月指标，
			Integer m = mMonth == 0?null:mMonth;
			if(unitType == 0) {//检修单位，有默认选中的单位名称
				faultFastRemoves.addAll(manager.queryMonthFaultFastRemoveByCondition(mYear, m, s, v, unitType, Arrays.asList(selectedMaintenance)));
			}else {
				faultFastRemoves.addAll(manager.queryMonthFaultFastRemoveByCondition(mYear, m, s, v, unitType));
			}
		}
	}

	public void resetFaultFastRemove() {
		initQueryCondition();
		queryFaultFastRemove();
	}

	public void editProtectCorrectAct() {
		protectCorrectAct = selectProtectCorrectAct;
	}

	public void saveProtectCorrectAct() {
		protectCorrectAct.setStatus(1); //人工创建或修改的
		if(protectCorrectAct.getActNum() == null || protectCorrectAct.getUnwantedActNum() == null
				||StringUtils.isEmpty( protectCorrectAct.getSubCompany())
				||protectCorrectAct.getVoltage() == null) {
			protectCorrectAct = null;
			selectProtectCorrectAct = null;
			addErrorMessage("请完整填写记录，添加失败");
			return;
		}
		if(protectCorrectAct.getActNum() == 0) {
			protectCorrectAct.setReality(100f);
		}else {
			protectCorrectAct.setReality(100f*(1f-(float)protectCorrectAct.getUnwantedActNum()/protectCorrectAct.getActNum()));
			if(protectCorrectAct.getReality() > 100) {
				protectCorrectAct.setReality(100f);
			}
		}
		if(protectCorrectAct.getPlan() > 100) {
			protectCorrectAct.setPlan(100f);
		}
		if (protectCorrectAct.getId() == null) {// 新建
			if(existSameActDate(protectCorrectAct.getYear(),protectCorrectAct.getMonth(),protectCorrectAct.getSubCompany(),protectCorrectAct.getVoltage())) {
				addErrorMessage("记录已存在，添加失败");
			} else {
				ProtectCorrectAct act = manager.createOrUpdate(protectCorrectAct);
				protectCorrectActs.add(act);
				addSuccessMessage("保存成功!");
			}
		} else {//编辑
			manager.createOrUpdate(protectCorrectAct);
			addSuccessMessage("保存成功!");
		}
		protectCorrectAct = null;
		selectProtectCorrectAct = null;
		queryProtectCorrectAct();
	}

	public void deleteProtectCorrectAct() {
		if (selectProtectCorrectAct != null && !selectProtectCorrectAct.getId().isEmpty()) {
			manager.deleteProtectCorrectActById(selectProtectCorrectAct.getId());
			queryProtectCorrectAct();
			selectProtectCorrectAct = null;
			addSuccessMessage("删除成功!");
		} else {
			addErrorMessage("请选择要删除的数据!");
		}
	}

	public void editFaultFastRemove() {
		faultFastRemove = selectFaultFastRemove;
	}

	public void saveFaultFastRemove() {
		faultFastRemove.setStatus(1); //人工创建或修改的
		if(faultFastRemove.getActNum() == null || faultFastRemove.getWantedActNum() == null
				||StringUtils.isEmpty( faultFastRemove.getSubCompany())) {
			addErrorMessage("请完整填写记录，添加失败");
			faultFastRemove = null;
			selectFaultFastRemove = null;
			return;
		}
		if(faultFastRemove.getActNum() == 0) {
			faultFastRemove.setReality(100f);
		}else {
			faultFastRemove.setReality(100f*((float)faultFastRemove.getWantedActNum()/faultFastRemove.getActNum()));
			if(faultFastRemove.getReality() > 100) {
				faultFastRemove.setReality(100f);
			}
		}
		if(faultFastRemove.getPlan() > 100) {
			faultFastRemove.setPlan(100f);
		}
		if (faultFastRemove.getId() == null) {// 新建
			if(existSameRemoveDate(faultFastRemove.getYear(),faultFastRemove.getMonth(),faultFastRemove.getSubCompany(),faultFastRemove.getVoltage())) {
				addErrorMessage("该记录已存在，新增失败");
			} else {
				FaultFastRemove act = manager.createOrUpdate(faultFastRemove);
				faultFastRemoves.add(act);
				addSuccessMessage("保存成功!");
			}
		} else {//编辑
			manager.createOrUpdate(faultFastRemove);
			addSuccessMessage("保存成功!");
		}
		faultFastRemove = null;
		selectFaultFastRemove = null;
		queryFaultFastRemove();
	}

	private boolean existSameRemoveDate(Integer year,Integer month,String sub,Integer voltage) {
		sub = "全部".equals(sub) || sub == null ? null:sub;
		List<FaultFastRemove> list;
		if(month == null) {
			list = manager.queryYearFaultFastRemoveByCondition(year, sub,voltage);
		}else {
			list = manager.queryMonthFaultFastRemoveByCondition(year,month, sub,voltage);
		}
		return list.size() > 0;
	}

	public void deleteFaultFastRemove() {
		if (selectFaultFastRemove != null && !selectFaultFastRemove.getId().isEmpty()) {
			manager.deleteFaultFastRemoveById(selectFaultFastRemove.getId());
			queryFaultFastRemove();
			addSuccessMessage("删除成功!");
			selectFaultFastRemove = null;
		} else {
			addErrorMessage("请选择要删除的数据!");
		}
	}

	/**
	 * 文件下载
	 */
	public void protectActFileDownloadView() {
		InputStream stream = null;
		try {
			stream = new FileInputStream(new File(pros.getTempletePath()+"protectKpi.xlsx"));
			templateFile = new DefaultStreamedContent(stream, "xlsx", "protectKpi.xlsx");
		} catch (FileNotFoundException e) {
			addErrorMessage("下载文件失败，请联系技术人员！");
			e.printStackTrace();
		}
	}
	public void faultRemoveFileDownloadView() {
		InputStream stream = null;
		try {
			stream = new FileInputStream(new File(pros.getTempletePath()+"faultRemoveKpi.xlsx"));
			templateFile = new DefaultStreamedContent(stream, "xlsx", "faultRemoveKpi.xlsx");
		} catch (FileNotFoundException e) {
			addErrorMessage("下载文件失败，请联系技术人员！");
			e.printStackTrace();
		}
	}

	public void chooseEquipment() {
		Map<String,Object> options = new HashMap<String, Object>();
		options.put("resizable", true);
		options.put("draggable", true);
		options.put("modal", true);
		options.put("width", 600);
		options.put("height", 200);

		Map<String,List<String>> params = new HashMap<>();
		List<String> param = new ArrayList<>();
		param.add("1");	//设置去除前几行
		params.put("ingnoreRows", param);
		openDialog("/ui/common/upload_excel_define", params, options);
	}

	public void onSelectorChosenProtectCorrectAct(SelectEvent event) {
		List<List<String>>objs = (List<List<String>>) event.getObject();
		try {
			for (List<String> list : objs) {
				//subCompany;plan;reality;month;year
				if(StringUtils.isBlank(list.get(0))) {
					continue;
				}
				ProtectCorrectAct pca = new ProtectCorrectAct();
				pca.setSubCompany((String) list.get(0));
				Float plan = Float.valueOf((String) list.get(1));
				plan = Math.min(plan, 100);
				pca.setPlan(plan);
				Integer actNum = Integer.valueOf((String) list.get(2));
				pca.setActNum(actNum);
				Integer unwantedActNum = Integer.valueOf((String) list.get(3));
				pca.setUnwantedActNum(unwantedActNum);
				if(actNum != 0) {
					pca.setReality(100f*(1f-(float)pca.getUnwantedActNum()/pca.getActNum()));
					if(pca.getReality() > 100) {
						pca.setReality(100f);
					}
				}else {
					pca.setReality(100f);
				}
				if(list.get(4) != null && StringUtils.isNotBlank(list.get(4).toString())) {
					int month = NumberFormat.getInstance().parse(list.get(4).toString()).intValue();
					pca.setMonth(month);
				}
				int year = NumberFormat.getInstance().parse(list.get(5).toString()).intValue();
				pca.setYear(year);
				int voltage = NumberFormat.getInstance().parse(list.get(6).toString()).intValue();
				pca.setVoltage(voltage);
				creatOrUpdateProtectCorrectAct(pca);
			}
			queryProtectCorrectAct();
			addSuccessMessage("文件上传成功！");
		}catch(Exception e) {
			e.printStackTrace();
			addErrorMessage("表格数字有误，请仔细核对！");
			return;
		}
	}

	public void creatOrUpdateProtectCorrectAct(ProtectCorrectAct pca) {
		List<ProtectCorrectAct> pcas = new ArrayList<>();
		if(pca.getMonth() == null) {
			pcas.addAll(manager.queryYearProtectCorrectActByCondition(pca.getYear(), pca.getSubCompany(),pca.getVoltage()));
		}else {
			pcas.addAll(manager.queryMonthProtectCorrectActByCondition(pca.getYear(), pca.getMonth(), pca.getSubCompany(),pca.getVoltage()));
		}

		if(pcas.size() != 0) { //存在
			pca.setId(pcas.get(0).getId());
		}
		manager.createOrUpdate(pca);
		queryProtectCorrectAct();
	}

	private boolean existSameActDate(Integer year, Integer month,String sub,Integer voltage) {
		sub = "全部".equals(sub) || sub == null ? null:sub;
		List<ProtectCorrectAct> list;
		if(month == null) {
			list = manager.queryYearProtectCorrectActByCondition(year, sub ,voltage);
		}else {
			list = manager.queryMonthProtectCorrectActByCondition(year,month, sub ,voltage);
		}
		return list.size() > 0;
	}

	public void onSelectorChosenFaultFastRemove(SelectEvent event) {
		List<List<String>>objs = (List<List<String>>) event.getObject();
		try {
			for (List<String> list : objs) {
				//subCompany;plan;reality;month;year;name
				if(StringUtils.isBlank(list.get(0))) {
					continue;
				}
				FaultFastRemove ffr = new FaultFastRemove();
				ffr.setSubCompany((String) list.get(0));
				Float plan = Float.valueOf((String) list.get(1));
				plan = Math.min(plan, 100);
				ffr.setPlan(plan);
				Integer actNum = Integer.valueOf((String) list.get(2));
				ffr.setActNum(actNum);
				Integer unwantedActNum = Integer.valueOf((String) list.get(3));
				ffr.setWantedActNum(unwantedActNum);
				if(actNum != 0) {
					ffr.setReality(100f*((float)ffr.getWantedActNum()/ffr.getActNum()));
					if(ffr.getReality() > 100) {
						ffr.setReality(100f);
					}
				}else {
					ffr.setReality(100f);
				}
				if(list.get(4) != null && StringUtils.isNotBlank(list.get(4).toString())) {
					int month = NumberFormat.getInstance().parse(list.get(4).toString()).intValue();
					ffr.setMonth(month);
				}
				int year = NumberFormat.getInstance().parse(list.get(5).toString()).intValue();
				ffr.setYear(year);
				int voltage = NumberFormat.getInstance().parse(list.get(6).toString()).intValue();
				ffr.setVoltage(voltage);
				creatOrUpdateFaultFastRemove(ffr);
			}
			queryFaultFastRemove();
			addSuccessMessage("文件上传成功");
		}catch(Exception e) {
			e.printStackTrace();
			addErrorMessage("表格数字有误，请仔细核对！");
			return ;
		}
	}

	public void creatOrUpdateFaultFastRemove(FaultFastRemove faultFastRemove) {
		List<FaultFastRemove> pcas = new ArrayList<>();
		if(faultFastRemove.getMonth() == null) {
			pcas.addAll(manager.queryYearFaultFastRemoveByCondition(faultFastRemove.getYear(), faultFastRemove.getSubCompany(),faultFastRemove.getVoltage()));
		}else {
			pcas.addAll(manager.queryMonthFaultFastRemoveByCondition(faultFastRemove.getYear(), faultFastRemove.getMonth(), faultFastRemove.getSubCompany(),faultFastRemove.getVoltage()));
		}
		if(pcas.size() != 0) { //存在
			faultFastRemove.setId(pcas.get(0).getId());
		}
		manager.createOrUpdate(faultFastRemove);
		queryFaultFastRemove();
	}

	public void queryChartData() {
		JSONArray rtn = new JSONArray();
		Integer v = voltage == 0?null:voltage;
		Integer m = month == 0?null:month;
		rtn.add(getProtectCorrectAct(year, m, v, unitType, Arrays.asList(selectedMaintenance)));
		rtn.add(getFaultFastRemove(year, m,v, unitType, Arrays.asList(selectedMaintenance)));
		if(month == 0) {
			rtn.add(year);
		}else {
			rtn.add(year+"-"+month);
		}
		String voltageLevel = v == null?"":v+"kV";
		rtn.add("{'voltageLevel':'"+voltageLevel+"'}");
		chartJsonData = rtn.toJSONString();
	}

	/**
	 * 根据年月电压等级获取故障快速切除率，并转为json
	 * @param year
	 * @param month
	 * @param voltage
	 * @return
	 */
	private String getFaultFastRemove(Integer year, Integer month,Integer voltage) {
		List<FaultFastRemove> ffrs = analysisM.getSummaryOfFaultFastRemoveBySubCompany(year, month, voltage);
		return analysisM.getFaultFastRemove(ffrs);
	}

	/**
	 * 根据年、月、电压等级、单位类型、单位名称获取故障快速切除率，并转为json
	 * @param year
	 * @param month
	 * @param voltage
	 * @param unitType 单位类型
	 * @param unit 单位名称
	 * @return
	 */
	private String getFaultFastRemove(Integer year, Integer month, Integer voltage, int unitType, List<String> unit) {
		List<FaultFastRemove> ffrs = new ArrayList<FaultFastRemove>();
		if(unitType == 0) {//检修单位，有默认选中的单位名称
			ffrs = analysisM.getSummaryOfFaultFastRemoveBySubCompany(year, month, voltage, unitType, unit);
		}else {
			ffrs = analysisM.getSummaryOfFaultFastRemoveBySubCompany(year, month, voltage, unitType);
		}
		return analysisM.getFaultFastRemoveTwo(ffrs, unitType, unit);
	}

	/**
	 * 根据年月电压等级获取保护正确动作率，并转为json
	 * @param year
	 * @param month
	 * @param voltage
	 * @return
	 */
	private String getProtectCorrectAct(Integer year, Integer month,Integer voltage) {
		List<ProtectCorrectAct> acts = analysisM.getSummaryOfCorrectActsBySubCompany(year, month, voltage);
		return analysisM.getProtectCorrectAct(acts);
	}
	/**
	 * 根据年、月、电压等级、单位类型、单位名称获取保护正确动作率，并转为json
	 * @param year
	 * @param month
	 * @param voltage
	 * @param unitType 单位类型
	 * @param unit 单位名称
	 * @return
	 */
	private String getProtectCorrectAct(Integer year, Integer month, Integer voltage, int unitType, List<String> unit) {
		List<ProtectCorrectAct> acts = new ArrayList<ProtectCorrectAct>();
		if(unitType == 0) {//检修单位，有默认选中的单位名称
			acts = analysisM.getSummaryOfCorrectActsBySubCompany(year, month, voltage, unitType, unit);
		}else{
			acts = analysisM.getSummaryOfCorrectActsBySubCompany(year, month, voltage, unitType);
		}
		return analysisM.getProtectCorrectActTwo(acts, unitType, unit);
	}

	public void selectYear() {
		FacesContext context = FacesContext.getCurrentInstance();
		Map<String, String> map = context.getExternalContext().getRequestParameterMap();
		String y = map.get("year").toString();
		year = Integer.valueOf(y);
	}
	public void selectQueryYear() {
		FacesContext context = FacesContext.getCurrentInstance();
		Map<String, String> map = context.getExternalContext().getRequestParameterMap();
		String y = map.get("mYear").toString();
		mYear = Integer.valueOf(y);
	}

	public void onRowSelectProtectCorrectAct(SelectEvent event){
		selectProtectCorrectAct = (ProtectCorrectAct) event.getObject();
	}

	public void onRowSelectFaultFastRemove(SelectEvent event){
		selectFaultFastRemove = (FaultFastRemove)event.getObject();
	}

	public List<String> completeSubCompany(String query) {
		return subCompanys;
	}

	public List<Integer> complateQueryMonths(Integer query) {
		return months;
	}

	public List<String> complateMonths(String query) {
		List<String> months = new ArrayList<String>();
		for (int i = 1; i <= 12; i++) {
			months.add(String.valueOf(i));
		}
		return months;
	}

	//自动统计保护正确动作率
	//	public void countAllProtectCorrectAct() {
	//		manager.deleteAllProtectCorrectAct();
	//		countM.countProtectCorrectAct();
	//		addSuccessMessage("自动统计成功");
	//		queryProtectCorrectAct();
	//	}

	//自动统计故障快速切除率
	//	public void countAllFaultFastRemove() {
	//		manager.deleteAllFaultFastRemove();
	//		countM.countFaultFastRemove();
	//		addSuccessMessage("自动统计成功");
	//		queryFaultFastRemove();
	//	}

	//统计当前年、月的保护正确动作率
	public void countCurrentProtectCorrectAct() {
		if(0==queryType) {
			// 统计年指标
			manager.deleteProtectCorrectAct(mYear, null, unitType, covered);
			for (Integer vol : voltages) {
				countM.countProtectCorrectAct(mYear, null, vol, unitType, covered);
			}
		} else if (1==queryType) {
			// 统计月指标
			manager.deleteProtectCorrectAct(mYear, mMonth, unitType, covered);
			for (Integer vol : voltages) {
				if(0==mMonth) {
					Calendar cal = Calendar.getInstance();
					int currentYear = cal.get(Calendar.YEAR);
					int currentMonth = cal.get(Calendar.MONTH)+1;
					if(mYear == currentYear) {
						// 如果为当前年，统计1-当前月的
						for (Integer i=1; i<=currentMonth; i++) {
							countM.countProtectCorrectAct(mYear, i, vol, unitType, covered);
						}
					} else {
						// 如果为历史年，统计1-12月份
						for (Integer month : months) {
							countM.countProtectCorrectAct(mYear, month, vol, unitType, covered);
						}
					}
				} else {
					countM.countProtectCorrectAct(mYear, mMonth, vol, unitType, covered);
				}
			}
		}
		queryProtectCorrectAct();
		addSuccessMessage("统计成功");
	}

	//统计当前年、月，故障快速切除率
	public void countCurrentFaultFastRemove() {
		if(0==queryType) {
			// 统计年指标
			manager.deleteFaultFastRemove(mYear, null, unitType, covered);
			for (Integer vol : voltages) {
				countM.countFaultFastRemove(mYear, null, vol, unitType, covered);
			}
		} else if (1==queryType) {
			// 统计月指标
			manager.deleteFaultFastRemove(mYear, mMonth, unitType, covered);
			for (Integer vol : voltages) {
				if(0==mMonth) {
					Calendar cal = Calendar.getInstance();
					int currentYear = cal.get(Calendar.YEAR);
					int currentMonth = cal.get(Calendar.MONTH)+1;
					if(mYear == currentYear) {
						// 如果为当前年，统计1-当前月的
						for (Integer i=1; i<=currentMonth; i++) {
							countM.countFaultFastRemove(mYear, i, vol, unitType, covered);
						}
					} else {
						// 如果为历史年，统计1-12月份
						for (Integer month : months) {
							countM.countFaultFastRemove(mYear, month, vol, unitType, covered);
						}
					}
				} else {
					countM.countFaultFastRemove(mYear, mMonth, vol, unitType, covered);
				}
			}
		}
		queryFaultFastRemove();
		addSuccessMessage("统计成功");
	}
	public List<Integer> getMonths() {
		return months;
	}

	public void setMonths(List<Integer> months) {
		this.months = months;
	}

	public List<String> getSubCompanys() {
		return subCompanys;
	}

	public void setSubCompanys(List<String> subCompanys) {
		this.subCompanys = subCompanys;
	}

	public String getSubCompany() {
		return subCompany;
	}

	public void setSubCompany(String subCompany) {
		this.subCompany = subCompany;
	}

	public List<ProtectCorrectAct> getProtectCorrectActs() {
		return protectCorrectActs;
	}

	public void setProtectCorrectActs(List<ProtectCorrectAct> protectCorrectActs) {
		this.protectCorrectActs = protectCorrectActs;
	}

	public ProtectCorrectAct getSelectProtectCorrectAct() {
		return selectProtectCorrectAct;
	}

	public void setSelectProtectCorrectAct(ProtectCorrectAct selectProtectCorrectAct) {
		this.selectProtectCorrectAct = selectProtectCorrectAct;
	}

	public ProtectCorrectAct getProtectCorrectAct() {
		return protectCorrectAct;
	}

	public void setProtectCorrectAct(ProtectCorrectAct protectCorrectAct) {
		this.protectCorrectAct = protectCorrectAct;
	}

	public List<FaultFastRemove> getFaultFastRemoves() {
		return faultFastRemoves;
	}

	public void setFaultFastRemoves(List<FaultFastRemove> faultFastRemoves) {
		this.faultFastRemoves = faultFastRemoves;
	}

	public FaultFastRemove getSelectFaultFastRemove() {
		return selectFaultFastRemove;
	}

	public void setSelectFaultFastRemove(FaultFastRemove selectFaultFastRemove) {
		this.selectFaultFastRemove = selectFaultFastRemove;
	}

	public FaultFastRemove getFaultFastRemove() {
		return faultFastRemove;
	}

	public void setFaultFastRemove(FaultFastRemove faultFastRemove) {
		this.faultFastRemove = faultFastRemove;
	}

	public StreamedContent getTemplateFile() {
		return templateFile;
	}

	public void setTemplateFile(StreamedContent templateFile) {
		this.templateFile = templateFile;
	}

	public Integer getYear() {
		return year;
	}

	public void setYear(Integer year) {
		this.year = year;
	}

	public Integer getMonth() {
		return month;
	}

	public void setMonth(Integer month) {
		this.month = month;
	}

	public Integer getQueryType() {
		return queryType;
	}

	public void setQueryType(Integer queryType) {
		this.queryType = queryType;
	}

	public String getChartJsonData() {
		return chartJsonData;
	}

	public void setChartJsonData(String chartJsonData) {
		this.chartJsonData = chartJsonData;
	}

	public Integer getmYear() {
		return mYear;
	}

	public void setmYear(Integer mYear) {
		this.mYear = mYear;
	}

	public Integer getmMonth() {
		return mMonth;
	}

	public void setmMonth(Integer mMonth) {
		this.mMonth = mMonth;
	}
	//
	//	public Integer getSelectType() {
	//		return selectType;
	//	}
	//
	//	public void setSelectType(Integer selectType) {
	//		this.selectType = selectType;
	//	}

	public Integer getVoltage() {
		return voltage;
	}

	public void setVoltage(Integer voltage) {
		this.voltage = voltage;
	}

	public List<Integer> getVoltages() {
		return voltages;
	}

	public void setVoltages(List<Integer> voltages) {
		this.voltages = voltages;
	}

	public int getUnitType() {
		return unitType;
	}

	public void setUnitType(int unitType) {
		this.unitType = unitType;
	}

	public String[] getSelectedMaintenance() {
		return selectedMaintenance;
	}

	public void setSelectedMaintenance(String[] selectedMaintenance) {
		this.selectedMaintenance = selectedMaintenance;
	}

	public Integer getmVoltage() {
		return mVoltage;
	}

	public void setmVoltage(Integer mVoltage) {
		this.mVoltage = mVoltage;
	}

	public boolean isCovered() {
		return covered;
	}

	public void setCovered(boolean covered) {
		this.covered = covered;
	}

}
